package com.oreilly.ez.fileserver.base;


import org.slf4j.MDC;
import org.springframework.stereotype.Component;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

@Component
public class FilterConfig implements Filter {
    @Override
    public void init(javax.servlet.FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest reqs = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "*");
//        response.setHeader("Access-Control-Max-Age", "5000");
//        response.setHeader("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,Authorization,Token");
        response.setHeader("Access-Control-Allow-Headers", "*");


//        long id = Thread.currentThread().getId();
//        UUID uuid = UUID.randomUUID();
//        String requestId = "thread id = "+String.valueOf(id) +", requestId = " + IdUtil.fastSimpleUUID();
        String requestId = req.getParameter("requestId");
        if(requestId == null || requestId.trim().isEmpty()){

//            response.setCharacterEncoding("UTF-8");
//            response.setContentType("application/json; charset=utf-8");
//
//
//            response.getWriter().append("{\n" +
//                    "  \"code\": -1,\n" +
//                    "  \"msg\": \"requestId is required\"\n" +
//                    "}");
//            return;
            requestId = UUID.randomUUID().toString();

        }

//        RequestHolder.setRequestId(requestId);
        MDC.put("requestId", requestId);

        filterChain.doFilter(req, res);
    }

    @Override
    public void destroy() {

    }


    /**
     * <p>
     * 跨域同源策略配置
     * </p>
     */
//    @Bean
//    public FilterRegistrationBean corsFilter() {
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        CorsConfiguration config = new CorsConfiguration();
//        config.setAllowCredentials(true);
//        config.addAllowedOrigin("http://localhost:8088");
//        config.addAllowedOrigin("http://crab.baomidou.com");
//        config.addAllowedHeader("*");
//        config.addAllowedMethod("*");
//        source.registerCorsConfiguration("/**", config);
//        return new CorsFilter(source);


//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        CorsConfiguration config = new CorsConfiguration();
//        config.setAllowCredentials(true);
//        // 设置你要允许的网站域名，如果全允许则设为 *
//        config.addAllowedOrigin("http://localhost:9200");
//        // 如果要限制 HEADER 或 METHOD 请自行更改
//        config.addAllowedHeader("*");
//        config.addAllowedMethod("*");
//        source.registerCorsConfiguration("/**", config);
//        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
//        // 这个顺序很重要哦，为避免麻烦请设置在最前
//        bean.setOrder(0);
//        return bean;
//    }

}
